package daoimpl;

import dao.DetailDAO;
import model.Detail;
import model.Order;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.List;

/**
 * Created by admin on 2016/8/15.
 */
public class DetailDAOImpl extends HibernateDaoSupport implements DetailDAO {
    private static final Logger log = LoggerFactory.getLogger(DetailDAOImpl.class);

    public void save(Detail detail) {
        log.debug("saving Detail instance");
        try {
            getHibernateTemplate().saveOrUpdate(detail);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    @Override
    public void update(Detail detail) {
        getHibernateTemplate().update(detail);
    }

    public List findAll() {
        log.debug("finding all Detail instances");
        try {
            String queryString = "from Detail";
            return getHibernateTemplate().find(queryString);
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    @Override
    public List findByOrder(int orderId) {
        return getHibernateTemplate().find("from Detail where orderId = " + orderId);
    }

    public Detail findById(int id) {
        log.debug("getting Detail instance with id: " + id);
        try {
            Detail instance = (Detail) getHibernateTemplate().get(
                    "model.Detail", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    @Override
    public void delete(Detail detail) {
        getHibernateTemplate().delete(detail);
    }

}
